<ul class="navigation">
<% if current_user.unposted_work && !params[:load_unposted] && !@use_upload_form -%>
  <li id='restore-link'><%= link_to 'Try to restore from last unposted draft?'.t, url_for(:action => :new, :load_unposted => true) -%></li>
<%- end -%>

<li id="upload-link" <%= @use_upload_form ? 'style="display: none;"' : '' -%> >
  <%= link_to_function('Upload from an existing URL?'.t,
  "Element.toggle('upload-work-form'); Element.toggle('work-form'); Element.toggle('upload-link'); 
   Element.toggle('form-link'); Element.toggle('restore-link')",
  :href => url_for(:controller => :works, :action => :new, :upload_work => true)) -%>
</li>
<li id="form-link"  <%= @use_upload_form ? '' : 'style="display: none;"' -%> >
  <%= link_to_function('Return to standard form'.t, 
  "Element.toggle('upload-work-form'); Element.toggle('work-form'); Element.toggle('upload-link'); 
  Element.toggle('form-link'); Element.toggle('restore-link')",
  :href => url_for(:controller => :works, :action => :new, :upload_work => false)) -%>
</li>
</ul>

<div id="upload-work-form"  <%= @use_upload_form ? '' : 'style="display: none;"' -%> > 
  <%= render :partial => 'works/upload_work_form' -%>   
</div>

<div id="work-form" class="work-form" <%= @use_upload_form ? 'style="display: none;"' : '' -%>>
<%- form_for(@work, :html => {:name => "storyForm"}) do |f| -%> 
  <p class="notice required"><%=h '* Required information'.t %></p>
  <%= render :partial => 'work_tags_form' -%>
  <fieldset>
    <legend><%= 'Story Preface'.t -%></legend>
    <dl class="preface">
      <dt class="work-title">
        <%= f.label :title, "Add Title*".t, :class => "required" %>
		<%= link_to_help "story-title" -%>
      </dt>
      <dd class="work-title">
        <%= f.text_field :title, :class => "storyinputfield" %>
        <%= live_validation_for_field('work_title', 
				      :maximum_length => ArchiveConfig.TITLE_MAX, :minimum_length => ArchiveConfig.TITLE_MIN, 
				      :failureMessage => 'We need a title! (At least %d letters long, please.)'.t/ArchiveConfig.TITLE_MIN)
				-%>
      </dd>
      
	<dt class="byline" <%= @pseuds.size > 1 ? "" : 'style="display: none;"' -%>>
	  <%= label :pseud, :id, "Select author/pseudonym(s)".t %>
	</dt>
	<dd class="byline" <%= @pseuds.size > 1 ? "" : 'style="display: none;"' -%>>
	  <select name="work[author_attributes][ids][]" multiple="multiple">
	    <%= options_from_collection_for_select(@pseuds, :id, :name, @selected_pseuds) %>
	  </select>	 
	</dd>
        <select name="work[author_attributes][coauthors][]" multiple="multiple" style="display : none;">
          <%= options_from_collection_for_select(@coauthors, :id, :name, @selected_pseuds) %>  
        </select>

        <% if @coauthors.size > 0 %>
       <%= "Current Co-authors: ".t  %>
          <ul>
          <% for author in @coauthors %>
            <li><%= h author.name %></li>
          <% end %>
          </ul>
    <% end %>
      <dt class="byline">
        <%= link_to_function('Add co-authors?'.t, "Element.toggle('co-authors')") %>          
      </dt>

      <dd id="co-authors">
     	    <%= f.label :storycoauthor, 'Enter co-author name(s):'.t %>
         	<%= text_field_with_auto_complete :pseud, :byline, { :size => 50 }, 
              { :url => {:controller => 'pseuds', :action => 'choose_coauthors'}, :method => :get, :param_name => 'search', 
                :tokens => ',', :min_chars => 2, :skip_style => true } %>            
      </dd>
      
      <dt class="summary">
        <%= f.label :summary, "Summary (max %d letters)".t/ArchiveConfig.SUMMARY_MAX %>
      </dt>
      <dd class="summary">
        <%= f.text_area :summary, :class => "summary-field" %>
        <%= live_validation_for_field('work_summary', :presence => false,
						:maximum_length => ArchiveConfig.SUMMARY_MAX) -%>
      </dd>

      <dt class="notes">
        <%= f.label :notes, "Notes (max %d letters)".t/ArchiveConfig.NOTES_MAX %>
      </dt>
      <dd class="notes">
        <%= f.text_area :notes, :class => "notes-field" %>
        <%= live_validation_for_field('work_notes', :presence => false,
						:maximum_length => ArchiveConfig.NOTES_MAX) -%>
      </dd>
</dl>
</fieldset>

<fieldset id="associations">
<legend><%=h 'Associations'.t -%></legend>
  <dl>
	<dt class="work-parent">
      <%= f.label :parent_url, "If your story is a remix or was inspired by another story in the archive, copy and paste that story's url below:".t %>
    </dt>
    <dd class="work-parent">
      <%= f.text_field :parent_url, :value => @work.url_for_parent, :live => true %>
    </dd>

  	<!-- Privacy dropdown (storyprivacy) -->
  	<dt class="work-restricted">
      <%= f.label :restricted, "Only show your work to registered users".t %> <%= link_to_help "registered-users" -%>
    </dt>
    <dd class="work-restricted">
      <%= f.check_box :restricted %>
    </dd>

    <dt class="work-is_serial">
      <%= f.label :storyseriescheck, "Is this part of a series?".t %> 
    </dt>
 	<dd class="work-is_serial">
     	<input type="checkbox" id="storyseriescheck" name="storyseriescheck"  onclick="showWorkSeriesOptions()" 
       <%- unless @work.series.blank? -%>checked="checked"<%- end -%>/>
    </dd>
    
    <!-- Series checkbox (storyseriescheck) -->
    <!-- Checking this box causes the "manage series" dropdown to appear; unchecking it causes it to disappear. -->
    <!-- Toggles on with series checkbox -->
    <dd id="seriesmanage">
      <fieldset>
        <legend><%= 'Manage Series'.t -%></legend>
        <dl>
          <% fields_for "work[series_attributes]" do |s| %>
            <dt><%= s.label 'series_id', 'Choose one of your existing series:'.t %>  <%= link_to_help "choosing-series" -%></dt>
            <dd>
				<%= s.collection_select(:id, @series, :id, :title, {:prompt => true}) %>	  
            </dd>
            <dt><%= s.label :title, "Or add a new one:".t %></dt>
            <dd><%= s.text_field :title, :class => "storyinputfield" %></dd>
      	  <%- end -%>
        	
        	<%- unless @serial_works.blank? -%>
        	  <dt><%= 'Current Series'.t -%></dt>
        	  <%- for serial in @serial_works -%>
        	    <dd>
        	      <ul class="navigation">
  				    <li><%= link_to serial.series.title, serial.series %></li> 
        	      	<li><%= link_to 'Remove'.t, serial, :confirm => 'Are you sure?'.t, :method => :delete %></li> 
        	      	<li><%= link_to 'Delete'.t, serial.series, :confirm => 'Are you sure?'.t, :method => :delete %></li>
  			      </ul>
        	    </dd>
      	    <%- end -%>
      	  <%- end -%>          
        </dl>
       </fieldset> 
    </dd>
    
  <% fields_for_associated('work', @work.chapters.first) do |c| %>
    <!-- Chaptered checkbox (work_is_wip) -->
    <!-- Checking this box causes the expected number of chapters box to appear; unchecking it causes it to disappear. -->
    <dt class="work-is_wip">
      <%= f.label :iswip, "Does this work have multiple chapters?".t %>
    </dt>
    <dd class="work-is_wip">
      <input type="checkbox" id="iswip" name="isWip"  
       onclick="showChapteredStoryOptions()" <%- if @work.chaptered? -%>checked="checked"<%- end -%>/>
    </dd>        
    <dd id="number-of-chapters">
    <p>
      <%=h 'Chapter 1 of'.t %> <%= f.text_field :wip_length, :class => "number-field" %> 
      <%=h 'Title:'.t %>
      <%=
      if @chapter != nil
	    	c.text_field :title, :value => @chapter.title 
			else
				c.text_field :title 
			end
			%>
    </p>
    </dd> 
	<!-- User-facing dates -->
	<dt class="work-is-backdated">
		<%= f.label :published_at, "Set a different publication date".t %>
	</dt>
	<dd>
	<input type="checkbox" id="publicationdatecheck" <%- if @work.published_at != @work.created_at -%>checked="checked"<%- end -%> />
	</dd>
	<dd id="managePublicationDate">
		<%= f.date_select "published_at", :start_year => Date.today.year, :end_year => 1950, :default => Date.today, :order => [:day, :month, :year] %>
	</dd>
	</dl>         
</fieldset>

<!-- Story text field (chapter_attributes_content) -->
<fieldset>
  <legend><%= 'Story Text'.t -%></legend>
  <p>
    <%= c.label :content, "Story, in plain text with limited HTML*".t, :class => "required" %><%= link_to_help "html-help" -%>
  </p>
	<p><code>a href, b, blockquote, br, center, cite, code, del, dd, dt, dl, div, em, h1, h2, h3, h4, h5, h6, i, ins, li, ol, pre, q, small, span, strike, strong, sub, sup, ul</code></p>
  <p class="rtf-html-switch">
	  <% use_tinymce -%>
    <span><a href="javascript:addEditor('content');"><%=h 'Rich text'.t %></a></span>
    <span><a href="javascript:removeEditor('content');">HTML</a></span>
  </p>      
  <p>
		<%= 
		if @chapter != nil
			c.text_area :content, :value => @chapter.content, :class => "mce-editor", :id => "content"
		else
			c.text_area :content, :class => "mce-editor", :id => "content"
		end
		%>
    <%= live_validation_for_field('content', 
				:maximum_length => ArchiveConfig.CONTENT_MAX, :minimum_length => ArchiveConfig.CONTENT_MIN, 
				:tooLongMessage => 'We salute your ambition! But sadly the content must be less than %d letters long. (Maybe you want to create a multi-chaptered work?)'.t/ArchiveConfig.CONTENT_MAX,
				:tooShortMessage => 'Brevity is the soul of wit, but your content does have to be at least %d letters long.'.t/ArchiveConfig.CONTENT_MIN,
			  :failureMessage => 'You did want to post a story here, right?'.t)
		-%>
  </p>      
</fieldset>

  <%- end -%> <!-- end of fields_for_associated -->

  <fieldset>
    <legend><%= 'Post Work'.t -%></legend>
    <p class="submit">
      <%= submit_tag 'Preview'.t, :name => 'preview_button' %>
      <%= submit_tag 'Cancel'.t, :name => 'cancel_button' %>
    </p> 
  </fieldset>
<%- end -%> <!-- end of form_for -->

</div>
